<template>
    <el-button v-if="getshow(scope, nodeItem)" :icon="nodeItem.icon" :type="nodeItem.btnType" :color="nodeItem.color"
        plain :size="nodeItem.size" @click=operClick(nodeItem,scope)>{{ nodeItem.name }}</el-button>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { parsingJson } from '@/utils/parsing'
import Api from '@/api/model/system'
import { ElNotification, ElMessageBox } from 'element-plus'
import {toTab} from './utils/hook.js'
const emit = defineEmits(['updateShow', 'updateTable', 'confirmDelClick', 'openDialog'])
const router = useRouter()
const props = defineProps({
    nodeItem: {
        type: Object,
        defalut: {}
    },
    scope: {
        type: Object,
        defalut: {}
    }
})
const dialogData = ref({
});
onMounted(() => {
})
const getshow = (scope, node) => {
    let buttonVisible = false
    if (!scope.row.show && node.showtype == '1') {
        buttonVisible = true
    }
    else if (scope.row.show == true && node.showtype == '2') {
        buttonVisible = true
    }
    else if (node.showtype == undefined) {
        buttonVisible = true
    }
    // else if()
    return buttonVisible
}


const operClick = (item, scope) => {
    let selectData = scope.row;
    dialogData.value = parsingJson(selectData, item);
    if (item.target == 'ajax') {
        if (item.promptStatement) {
            ElMessageBox.confirm(item.promptStatement, '提示', {
                type: 'warning',
                confirmButtonText: '确定',
                confirmButtonClass: 'el-button--danger'
            }).then(() => {
                emit('confirmDelClick', item.url, dialogData.value)
            }).catch(() => { })
        }
        else {
            emit('confirmDelClick', item.url, dialogData.value)
        }
    }
    else if (item.target == 'dialog') {
        emit('openDialog', item, dialogData.value)
    }
    else if (item.target == 'update') {
        updateClick(item.url, scope.row)
    }
    else if (item.target == 'tab') {
        toTab(item, scope.row,router);
    }
    if (item.showtype == '1') {
        emit('updateShow', true)
    }
    else if (item.showtype == '2' && item.target != 'update') {
        emit('updateShow', false)
    }
}

const updateClick = async (url, row) => {
    let params = {
        bean: row
    }
    const res = await Api.systemDelete.post(url, params);
    if (res.code == 200) {
        ElNotification.success({ title: '', message: '成功！', duration: 2000 });
        emit('updateShow', false)
    }
    else {
        ElNotification.error({ title: '失败', message: res.msg });
    }
}
</script>
